import React, { Component } from 'react';
import CustomTable from './components/CustomTable';
import CustomRsuiteTb from './components/CustomRsuiteTb';
import { Feedback } from '@icedesign/base';
import axios from 'axios';

export default class TabReqParam extends Component{
    static displayName = 'TabTable';
    static propTypes = {};
    static defaultProps = {};

    constructor(props) {
        super(props);
        this.state = {
            dataSource: [{"desc": " ", "name": " ", "required": " ", "type": " "}],
            param:{
                actionName:"",
                method:"",
                paramType:"",
            }
        };
        this.columns = [
            {
                title: '参数名',
                dataIndex: 'name',
                key: 'name',
                width: 200,
            },
            {
                title: '参数类型',
                dataIndex: 'type',
                key: 'type',
                width: 200,
            },
            {
                title: '是否必填',
                dataIndex: 'required',
                key: 'required',
                width: 200,
            },
            {
                title: '描述',
                dataIndex: 'desc',
                key: 'desc',
                width: 150,
            },
        ];
    }

    componentDidUpdate(){
        if (this.state.param.actionName != this.props.action ||
            this.state.param.method != this.props.method ||
                this.state.param.paramType != this.props.paramType
        ){
            var params = new URLSearchParams();
            params.append('actionName', this.props.action);
            params.append('method', this.props.method);
            params.append('paramType', this.props.paramType);
            this.setState({param:{
                actionName:this.props.action,
                method:this.props.method,
                paramType:this.props.paramType,
            }});
            axios
                .post('/api/getReqParam',params)
                .then((response) => {
                    this.setState({
                        dataSource: response.data
                    });
                })
                .catch((error) => {
                    Feedback.toast.error(error.msg);
                });
        }
    }

    componentDidMount() {
        var params = new URLSearchParams();
        params.append('actionName', this.props.action);
        params.append('method', this.props.method);
        params.append('paramType', this.props.paramType);
        this.setState({param:{
            actionName:this.props.action,
            method:this.props.method,
            paramType:this.props.paramType,
        }});
        axios
            .post('/api/getReqParam',params)
            .then((response) => {
                this.setState({
                    dataSource: response.data
                });
            })
            .catch((error) => {
                Feedback.toast.error(error.msg);
            });
    }

    render(){
        return(
              <CustomTable
                  dataSource={this.state.dataSource}
                  columns={this.columns}
                  hasBorder={false}
                  isTree={true}
              />
        );
    }
}
